package com.firefly.app.cordova.plugins;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;

import android.app.Activity;
import android.content.Intent;

import com.firefly.app.biz.order.dto.OrderDTO;
import com.firefly.app.biz.order.dto.OrderResultDTO;
import com.firefly.app.biz.order.dto.PayDTO;
import com.firefly.app.biz.order.dto.PayTransDTO;
import com.firefly.app.biz.order.service.OrderService;
import com.firefly.app.biz.order.service.OrderServiceImpl;
import com.firefly.mobile.app.util.SytLog;

public class OrderPlugin  extends CordovaPlugin implements CordovaInterface{
		private final ExecutorService threadPool = Executors.newCachedThreadPool(); 
	 
	    /** 
	     * 注意 构造方法不能为 
	     *  
	     * HomePlugin(){}   
	     * 可以不写或者 定义为如下 
	     *   
	     */  
	    public OrderPlugin() {  
	    }  
	    
	    private OrderService orderService;
	  
	    @Override  
	    public boolean execute(final String action,final  org.json.JSONArray args,  
	    		final  CallbackContext callbackContext) throws org.json.JSONException {  
	        SytLog.i("orderPlugin", action);  
	        
	        cordova.getThreadPool().execute(new Runnable() {
	            public void run() {
	                try {
						execPlugin(action, args, callbackContext);
					} catch (Exception e) {
						e.printStackTrace();
						SytLog.e("AdsPlugin", "插件调用出错");
						callbackContext.error("插件调用出错");
					}
	            }
	        });
	        
	        return true;  
	  
	    }  
	    
	    private void execPlugin(final String action,final  org.json.JSONArray args,  
	    		final CallbackContext callbackContext) throws org.json.JSONException {
	        try {
	        	if (orderService == null) {
	        		orderService = new OrderServiceImpl();
				}
				if (action.equals("createOrder")) {  
					 // 获取JS传递的args的第一个参数  
					String bizInfo = args.getString(0);
				    // 获取JS传递的args的第一个参数  
				    SytLog.e("orderPlugin", "接收js调用"+action+"内容"+bizInfo); 
				    
					OrderDTO orderDTO  = com.alibaba.fastjson.JSONObject.parseObject(bizInfo, OrderDTO.class);
					
					OrderResultDTO orderResultDTO = orderService.createOrder(orderDTO);
					SytLog.i("orderPlugin","订单中心下单返回："+ orderResultDTO);
					
			    	callbackContext.success(com.alibaba.fastjson.JSONObject.toJSONString(orderResultDTO)); 
			        return; 
				}else if (action.equals("pay")) {  
					// 获取JS传递的args的第一个参数  
					String bizInfo = args.getString(0);
				    // 获取JS传递的args的第一个参数  
				    SytLog.e("orderPlugin", "接收js调用"+action+"内容"+bizInfo); 
				    PayTransDTO payTransDTO  = com.alibaba.fastjson.JSONObject.parseObject(bizInfo, PayTransDTO.class);
				    PayDTO payDTO = new PayDTO();
				    payDTO.setPayType(payTransDTO.getCardKind());
				    payDTO.setTradeNo(payTransDTO.getTradeNo());
				    payDTO.setPayAmt(payTransDTO.getPayAmt());
				    payDTO.setTransAmt(payTransDTO.getTransAmt());
				    payDTO.setPayData(payTransDTO.getPayData());
					OrderResultDTO payResultDTO = orderService.pay(payDTO);
					SytLog.i("orderPlugin","订单中心支付返回："+ payResultDTO);
			    	callbackContext.success(com.alibaba.fastjson.JSONObject.toJSONString(payResultDTO)); 
			        return; 
				}else if (action.equals("queryOrder")) {  
					 // 获取JS传递的args的第一个参数  
					String bizInfo = args.getString(0);
				    // 获取JS传递的args的第一个参数  
				    SytLog.e("orderPlugin", "接收js调用"+action+"内容"+bizInfo); 
				    
					OrderDTO orderDTO  = com.alibaba.fastjson.JSONObject.parseObject(bizInfo, OrderDTO.class);
					
					OrderResultDTO orderResultDTO = orderService.queryOrder(orderDTO);
					SytLog.i("orderPlugin","订单中心下单返回："+ orderResultDTO);
					
			    	callbackContext.success(com.alibaba.fastjson.JSONObject.toJSONString(orderResultDTO)); 
			        return; 
				}
			} catch (Exception e) {
				e.printStackTrace();
				SytLog.e("orderPlugin", "调用订单中心出错");
			}  
	    }
	    
		@Override
		public void startActivityForResult(CordovaPlugin command,
				Intent intent, int requestCode) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void setActivityResultCallback(CordovaPlugin plugin) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public Activity getActivity() {
			// TODO Auto-generated method stub
			return this.cordova.getActivity();
		}

		@Override
		public ExecutorService getThreadPool() {
			return threadPool;
		}
	  
}
